'use strict';

stareal
    .controller("venusSearchController", function ($scope, $api, $stateParams, $timeout, $lazyLoader,localStorageService,$alert) {
        // 展现热词
        $scope.hwf = true;
        $scope.longitude=localStorageService.get('longitude');//当前的位置
        $scope.latitude=localStorageService.get('latitude');//当前的位置
        var h=$(window).height()-1.4*parseFloat(document.documentElement.style.fontSize)+"px";
        $("#wrapper_container").height(h);
        //历史搜索
        $scope.history_list =new Array(8);
        if(localStorageService.get("venussearchlist")!=undefined&&localStorageService.get("venussearchlist")!=null&&localStorageService.get("venussearchlist")!='') {
            $scope.history_list = JSON.parse(localStorageService.get("venussearchlist"));
        }else{
            $scope.history_list=[];
        }
        if($scope.history_list){
            $scope.history = $scope.history_list;
        }else{
            $scope.history = [];
        }
        // console.log($scope.history)
        // $scope.history_list;
        //搜索方法
        var search = function(keyword){
            $timeout(function () {
                $scope.hwf = false;
                $scope.hwe = false;
                $scope.venusgoods = new $lazyLoader("app/site/list",{
                    name: keyword,
                    cityid:'',
                    distance:'',
                    score:'',
                    cityName:'',
                    longitude:$scope.longitude,
                    latitude: $scope.latitude
                });
                $scope.venusgoods.nextPage();
            }, 0);
        };
        $scope.getDistance=function(long,lat){
            // console.log(long);
            // console.log(lat);
            if(long==undefined||long==''||long==null||lat==undefined||lat==''||lat==null){
                return '暂无'
            }else{
                var p1 = [long, lat];
                var p0 = [ $scope.longitude,$scope.latitude];
                var dis = AMap.GeometryUtil.distance(p1, p0); //米
                return (dis/1000).toFixed(2)+'km' ;
            }

        }
        //点击热词搜索
        $scope.hws = function(keyword){
            $scope.keyword = keyword;
            search(keyword);
        };
        //回车搜索
        var searchBtn = document.getElementById("searchBtn");
        var sou_iocnBtn1=document.getElementById('sou_iocnBtn1');
        var searchClear_icon1=document.getElementById('searchClear_icon1');
        searchBtn.onsearch = function(event){
            event.preventDefault();
            var keyword = this.value;
            if(keyword!=''&&keyword!=null&&keyword!=undefined) {
                search(keyword);
                $scope.history.unshift({"word": keyword});
                localStorageService.set("venussearchlist", JSON.stringify($scope.history));
            }else{
                $timeout(function () {
                    $alert.show('搜索内容不能为空!');
                },0)
            }
        };
        sou_iocnBtn1.onclick=function(){
            var keyword = $('#searchBtn').val();
            if(keyword!=''&&keyword!=null&&keyword!=undefined) {
                search(keyword);
                $scope.history.unshift({"word": keyword});
                localStorageService.set("venussearchlist", JSON.stringify($scope.history));
            }else{
                $timeout(function () {
                    $alert.show('搜索内容不能为空!');
                },0)
            }
        }
        searchClear_icon1.onclick=function(){
            $('#searchBtn').val('');
        }
        //输入没有内容
        searchBtn.oninput = function () {
            if(this.value.length<1){
                $scope.hwe = true;
                $scope.hwf = true;
                $scope.venusgoods.nodata = false;
                $scope.history_list = JSON.parse(localStorageService.get("venussearchlist"));
            }
        }
        $scope.dele = function (id) {
            $scope.history_list.splice(id,1 );
            $scope.history.splice(id,1 );
            localStorageService.set("venussearchlist",JSON.stringify($scope.history_list));
        }
        $scope.clearAll=function(){
            $scope.history_list=[];
            $scope.history=[];
            localStorageService.set("venussearchlist",'');
        }
        // localStorageService.remove("list")
    });